package com.salon.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.salon.server.entity.Product;
import com.salon.server.model.R;
import com.salon.server.model.request.ProductRequest;
import com.salon.server.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/salon/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("page")
    public R page(@RequestBody ProductRequest productRequest) {
        return R.success(productService.page(productRequest, new LambdaQueryWrapper<Product>()
                .eq(productRequest.getStatus() != null, Product::getStatus, productRequest.getStatus())
                .like(productRequest.getName() != null, Product::getName, productRequest.getName())
                        .eq(productRequest.getCategory() != null, Product::getCategory, productRequest.getCategory())
                )
        );
    }

    /**
     * 新增
     */
    @PostMapping("add")
    public R add(@RequestBody Product product) {
        return R.success(productService.save(product));
    }

    /**
     * 更新
     */
    @PostMapping("update")
    public R update(@RequestBody Product product) {
        return R.success(productService.updateById(product));
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    public R delete(@RequestBody Product product) {
        return R.success(productService.removeById(product));
    }

    /**
     * list
     */
    @PostMapping("list")
    public R list(@RequestBody ProductRequest productRequest) {
        return R.success(productService.list(new LambdaQueryWrapper<Product>()
                .eq(productRequest.getCategory() != null, Product::getCategory, productRequest.getCategory())
        ));
    }

}
